package cn.scu.ne04jdemo.util;

import java.util.List;

public class PageUtil {
    public static List page(List list, int pageNum, int pageSize) {
        if (list == null || list.size() == 0) {
            return list;
        }

        int total = list.size(); // 记录总数
        int pageTotal = 0; // 页数
        if (total % pageSize == 0) {
            pageTotal = total / pageSize;
        } else {
            pageTotal = total / pageSize + 1;
        }

        int fromIndex = 0; // 开始索引
        int toIndex = 0; // 结束索引

        if (pageNum != pageTotal) {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = fromIndex + pageSize;
        } else {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = total;
        }

        if (fromIndex >= total)
            return null;
        List pageList = list.subList(fromIndex, toIndex);
        return pageList;
    }
}
